# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ignacio Casal Quinteiro <icq@gnome.org>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=glib2
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=2.47.4.13.gea5ca11
provides=(${MINGW_PACKAGE_PREFIX}-${_realname}=$pkgver)
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname})
pkgrel=1
url="http://www.gtk.org/"
arch=('any')
pkgdesc="Common C routines used by GTK+ 2.4 and other libs (git) (mingw-w64)"
license=(LGPL2)
options=('debug' '!strip')
install=glib2-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-python2")
source=("${_realname}::git+https://git.gnome.org/browse/glib"
        0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch
        0003-g_abort.all.patch
        0004-glib-prefer-constructors-over-DllMain.patch
        0017-glib-use-gnu-print-scanf.patch
        0023-print-in-binary-more-for-testing.all.patch
        0027-no_sys_if_nametoindex.patch
        0028-inode_directory.patch)

sha256sums=('SKIP'
            'ef81e82e15fb3a71bad770be17fe4fea3f4d9cdee238d6caa39807eeea5da3e3'
            '1b24cc928f69f73599f83269a7b3eb7bf7efbe114109251e6765053a1e1f4cd6'
            '7b099af0c562f397458542482d6d1debe437f220762aa2ed94b2e6c4d43dd8a6'
            'e916e4913632485b314269a5c8888e2eff25bb7034f244bc235c08ba7f28b5ea'
            'd1d35bb7865527422e28635ed373731e800c30ff5732c7bf8c66be37158509fb'
            '5cb481295ff86c2802030984d8b2bf6a3b1dcd5e5fe7b0be68b22d9116305837'
            'f7f06a90156fe0a308412512c359072922f7f0d19dd4bed30d863db18e48940b')

pkgver() {
  cd "${srcdir}"/${_realname}
  git describe --tags | sed 's|-|.|g'
}

prepare() {
  cd "${srcdir}"/${_realname}

  patch -Np1 -i "${srcdir}"/0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch
  patch -Np1 -i "${srcdir}"/0003-g_abort.all.patch
  patch -Np1 -i "${srcdir}"/0004-glib-prefer-constructors-over-DllMain.patch
  patch -Np1 -i "${srcdir}"/0017-glib-use-gnu-print-scanf.patch
  patch -Np1 -i "${srcdir}"/0023-print-in-binary-more-for-testing.all.patch
  patch -Np1 -i "${srcdir}"/0027-no_sys_if_nametoindex.patch
  patch -Np1 -i "${srcdir}"/0028-inode_directory.patch

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  export MSYS2_ARG_CONV_EXCL="-//OASIS//DTD"
  msg "Build shared version"
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared
  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-static --enable-shared \
    --disable-libelf \
    --with-python=${MINGW_PREFIX}/bin/python.exe \
    --with-threads=posix \
    --with-xml-catalog=${MINGW_PREFIX}/etc/xml/catalog
  make

  msg "Build static version"
  mkdir "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static
  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-shared --enable-static \
    --disable-libelf \
    --with-python=${MINGW_PREFIX}/bin/python.exe \
    --with-threads=posix \
    --with-xml-catalog=${MINGW_PREFIX}/etc/xml/catalog
  make
}

package() {
  cd "${srcdir}"/build-${CARCH}-static
  make DESTDIR="${pkgdir}"/static install

  cd "${srcdir}"/build-${CARCH}-shared
  make DESTDIR="${pkgdir}" install

  mv "${pkgdir}"/static/${MINGW_PREFIX}/lib/*.a "${pkgdir}"/${MINGW_PREFIX}/lib/

  rm -rf "${pkgdir}"/static

  rm -f "${pkgdir}"${MINGW_PREFIX}/lib/charset.alias
  #rm -rf "${pkgdir}"${MINGW_PREFIX}/{share/{gtk-doc,man,bash-completion,gdb},lib/charset.alias,etc}
  pushd "${pkgdir}"${MINGW_PREFIX}/lib
    find . -maxdepth 1 -name "*.def" -exec rm -f {} \;
  popd

  #rm -f "${pkgdir}"${MINGW_PREFIX}/bin/gdbus-codegen
  #rm -rf "${pkgdir}"${MINGW_PREFIX}/lib/gdbus-2.0

  install -Dm644 "${srcdir}"/${_realname}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
